﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Application.Services;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Abp.UI;
using XHD.WXZJ.Authorization.Roles;
using XHD.WXZJ.Menus.Dto;
using XHD.WXZJ.Users;

namespace XHD.WXZJ.Menus
{
    public class MenuAppService : ApplicationService,IMenuAppService
    {
        private readonly IUnitOfWorkManager _unitOfWorkManager;
    
       private readonly IRepository<Menu,int> _menuRepository;

        public MenuAppService(IRepository<Menu,int> menuRepository,
           
            IUnitOfWorkManager unitOfWorkManager
            )
        {
            _unitOfWorkManager = unitOfWorkManager;
         
            _menuRepository = menuRepository;
        }
        /// <summary>
        /// 创建菜单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task CreateMenu(CreateMenuInput input)
        {
            var menu = new Menu
            {
                ParentId = input.ParentId,
                Name = input.Name,
                DisplayName = input.DisplayName,
                MenuUrl = input.MenuUrl,
                MenuIcon = input.MenuIcon
            };
             var exitsMenu = _menuRepository.GetAllList().Find(m => m.Name == input.Name );
            if (exitsMenu == null)
            {

                await _menuRepository.InsertOrUpdateAndGetIdAsync(menu);
                _unitOfWorkManager.Current.SaveChanges();
            }
            else
            {
                throw new UserFriendlyException("已经存在相同名称的菜单，请更换菜单名称！");
            }
           
           
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task DeleteMenu(MenuInput input)
        {
            var menu = _menuRepository.GetAllList().Find(m => m.Name == input.MenuName && m.Id == input.MenuId);
            if (menu != null)
            {
                await _menuRepository.DeleteAsync(m=>m.Id == menu.Id);
            }
        }
        /// <summary>
        /// 获取所有菜单
        /// </summary>
        /// <returns></returns>
       public IList<MenuDto> GetAllMenus()
        {
          

           var menus = _menuRepository.GetAllList();
           var meusList = menus.MapTo<List<MenuDto>>();
           return meusList;
        }

        /// <summary>
        /// 更新菜单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
       public async Task UpdateMenu(CreateMenuInput input)
       {
           var menu = await _menuRepository.FirstOrDefaultAsync(m => m.Name == input.Name);
           if (menu != null)
           {
               if (!string.IsNullOrEmpty(input.DisplayName))
               {
                   menu.DisplayName = input.DisplayName;
               }
               if (!string.IsNullOrEmpty(input.MenuUrl))
               {
                   menu.MenuUrl = input.MenuUrl;
               }
               if (string.IsNullOrEmpty(input.MenuIcon))
               {
                   menu.MenuIcon = input.MenuIcon;
               }
              
               
               await _menuRepository.UpdateAsync( menu);
               _unitOfWorkManager.Current.SaveChanges();
           }
           else
           {
               throw new UserFriendlyException("There is no such a menu. Maybe it's deleted.");
           }
       }

        public IList<MenuDto> GetChildrenMenusById(int menuId)
        {
            var menus = _menuRepository.GetAll().ToList().Where(m=>m.ParentId == menuId);
            return menus.MapTo<List<MenuDto>>();
           
        }

    }
}
